package com.xuanyuan.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xuanyuan.domain.AjaxResult;
import com.xuanyuan.entity.Employee;
import com.xuanyuan.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

@Validated
@Controller
@RequestMapping("/api/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @ResponseBody
    @RequestMapping("/page")
    public AjaxResult selectEmployeePage(Employee employee, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return AjaxResult.success(new PageInfo<>(employeeService.selectEmployeeList(employee)));
    }

    @ResponseBody
    @RequestMapping("/list")
    public AjaxResult selectEmployeeList(Employee employee) {
        return AjaxResult.success(employeeService.selectEmployeeList(employee));
    }

    @ResponseBody
    @PostMapping("/add")
    public AjaxResult insertEmployee(@Validated Employee employee) {
        return AjaxResult.toAjax(employeeService.insertEmployee(employee));
    }

    @ResponseBody
    @PostMapping("/edit")
    public AjaxResult updateEmployee(@Validated Employee employee, @NotNull(message = "员工id不能为空") Integer id) {
        //必须需要id
        employee.setId(id);
        return AjaxResult.toAjax(employeeService.updateEmployee(employee));
    }

    @ResponseBody
    @PostMapping("/removes")
    public AjaxResult deleteEmployeeByIds(@NotEmpty(message = "员工ids不能为空") String[] ids) {
        return AjaxResult.toAjax(employeeService.deleteEmployeeByIds(ids));
    }
}

